<template>
  <view>
    <uni-popup ref="popup" type="center" :maskClick="false">
      <view class="popup_box">
        <view class="close" @click="closeMyPopup">×</view>
        <view class="please">请输入手机号码免费接收查询结果</view>
        <view class="phone_box">
          <view class="phone">手机号</view>
          <input type="number" class="number" placeholder="请输入手机号码" v-model.trim="phone" />
        </view>
        <view class="code_box">
          <view class="code">验证码</view>
          <input type="number" class="code_number" placeholder="请输入验证码" v-model.trim="code" />
          <view class="send_code">
            <text class="countdown_code" v-if="send" @click="sendCode">发送验证码</text>
            <text class="countdown" v-else>{{ number }}秒后重发</text>
          </view>
        </view>
        <view class="btn" @click="sendInfoHandle">接收查询结果</view>
      </view>
    </uni-popup>
  </view>
</template>
<script>
import uniPopup from "@/components/uni-popup/uni-popup.vue";
export default {
  props: {
    content: {
      type: Object,
    },
    showStatus: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    uniPopup,
  },
  data() {
    return {
      number: 60,
      send: true,
      code: "",
      phone: "",
    };
  },
  mounted() {

    //
    			if (uni.getStorageSync('userInfo') && uni.getStorageSync('userInfo') != 'null') {
                    this.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
                    this.phone = this.userInfo.phone
        
        }
  
    if (this.showStatus) {
      this.$refs.popup.open();
    }
  },
  methods: {
    checkPhone() {
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone)) {
        uni.showToast({
          title: "请输入有效手机号码",
          icon: "none",
          duration: 2000,
        });
        return false;
      } else {
        return true;
      }
    },

    sendInfoHandle() {
      if (this.checkPhone()) {
        // 验证码
        if (this.code == "") {
          uni.showToast({
            title: "请输入验证码",
            icon: "none",
            duration: 2000,
          });

          return false;
        }
        // checkCode
        var data = {
          phone: this.phone,
          code: this.code,
        };

        this.$http.post("/api/checkSend", data).then((res) => {
          if (res.data.code == 20000) {
            this.sendCatogoryInfo();
          } else {
            uni.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000,
            });
          }
        });
      }
    },
    // 提交 所有信息
    sendCatogoryInfo() {
      // 动态添加对象值
      var obj = {
        contactNumber: this.phone,
        contactUser: "", //联系人
      };
      var new_obj = { ...this.content, ...obj };
      console.log(new_obj)
      this.$http.post("/api/categoryMessage", new_obj).then((res) => {
        if (res.data.code == 20000) {
          this.closeMyPopup();
          uni.showToast({
            title: "提交成功",
            duration: 2000,
          });
        }
      });
    },
    sendCode() {
      if (this.checkPhone()) {
        this.send = false;
        var _this = this;
        this.getCode();
        let t = setInterval(() => {
          this.number--;
          if (this.number == 0) {
            this.send = true;
            // this.getCode();
            clearInterval(t);
            this.number = 60;
          }
        }, 1000);
        if (this.number == 0) {
          clearInterval(t);
        }
      }
    },

    getCode() {
      this.$http.post("/api/send", { phone: this.phone }).then((res) => {
        if (res.data.code == 200) {
            console.log("发送成功");
        }
      });
    },

    closeMyPopup() {
      this.$refs.popup.close();
      // 清空值
      this.send = true;
      this.code = "";
      this.phone = "";
      console.log("关闭");
      // 传值
      this.$emit("childFn", false);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
